import Vue from 'vue'
import Vuex from 'vuex'
// 持久化插件
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    loginData: {}             // 共享的用户数据
  },
  getters: {
        getUserId(state) {
            return state.loginData.user ? state.loginData.user.id : 0
        },
        getUser(state) {
            return state.loginData.user || {}
        },
        getBearerToken(state) {
            return state.loginData.token ? 'Bearer ' + state.loginData.token : ''
        },
        getToken(state) {
            return state.token || ""
        }
  },
  mutations: {
    setLoginData(state,loginData) {   // 保存用户数据
      state.loginData = loginData
    },

     setUser(state,user) {
       state.loginData.user = JSON.parse(JSON.stringify(user))
     }

  },
  actions: {
  },
  modules: {
  },

  //每次在vueX中存储值的时候，自动在本地给你存储对应的值 -- 插件
  plugins: [createPersistedState({  // 持久化存储
    storage: window.localStorage,     //存储方式：localStorage、sessionStorage、cookies
    key: "user"
  })]
})
